package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.AparticleConfigMapper;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.article.dto.ArticleHomeDto;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.article.pojo.ApArticleConfig;
import com.zhentao.model.article.pojo.ApArticleContent;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApArticleContentService;
import com.zhentao.service.ArticleService;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.Date;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private AparticleConfigMapper aparticleConfigMapper;
    @Autowired
    private ApArticleContentService apArticleContentService;
    //设置size常量   当页面传递过来的>50的时候，默认设置的最大数是50
    private final static short MAX_PAGE_SIZE=50;

    @Override
    public ResponseResult load(Integer loadType, ArticleHomeDto dto) {
        //1.校验参数 -size
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        //如果size<50 size=size  如果size>50  size=50
        size = Math.min(size, MAX_PAGE_SIZE);
        //把size赋值给dto
        dto.setSize(size);

        //2.校验参数 -频道
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        //3.校验参数 -时间
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }

        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }

        //2.查询数据
       List<ApArticle> apArticleList= articleMapper.loadArticleList(dto,loadType);

        //3.封装结果
        return ResponseResult.okResult(apArticleList);

    }

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
//        1 校验参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

//        2 封装参数
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);

//        进行保存 判断是新增还是修改 id
        if (dto.getId() == null) { //新增
//            保存文章基本信息
            articleMapper.insert(apArticle);
//            保存内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);

//            保存配置表 -自己实现
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            aparticleConfigMapper.insert(apArticleConfig);
        }else {
//            修改文章
            articleMapper.updateById(apArticle);

//            修改内容
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent content = apArticleContentMapper.selectOne(queryWrapper);
            content.setContent(dto.getContent());
            apArticleContentMapper.updateById(content);
        }

        apArticleContentService.createStaticUrl(apArticle.getId());

        return ResponseResult.okResult(apArticle.getId());
    }
}